package com.wpfap.kfzapp;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;

import android.graphics.Bitmap;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;


public class TankenActivity extends Activity {
    private Object rechnungButton;
	private Button speichernButton;
	public  Bitmap pictureTaken;
	public  Intent ImageIntent;
	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tanken);
        
        Spinner spinner = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.tankstellen_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        
        this.rechnungButton = (Button)this.findViewById(R.id.button2);
        ((Button) this.rechnungButton).setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
              ImageIntent = new Intent("android.media.action.IMAGE_CAPTURE");
              startActivityForResult(ImageIntent, 0);
          }
        });
        this.speichernButton = (Button)this.findViewById(R.id.button1);
        ((Button) this.speichernButton).setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
        	  System.out.println("Jetzt wird gespeichert .... ");
        	  Date jetzt = new Date();
        	  String name=""+jetzt.getDay()+"_"+jetzt.getMonth()+"_"+jetzt.getYear()+"_"+jetzt.getHours()+"_"+jetzt.getMinutes()+"_"+jetzt.getSeconds();
        	  File file = new File(Environment.getExternalStorageDirectory()+"/wpfap/daten_"+name);
  			FileWriter fw;
			try {
				fw = new FileWriter(file);
				BufferedWriter out = new BufferedWriter(fw);
	  			out.write(((EditText)findViewById(R.id.editText1)).getText().toString());
	  			out.newLine();
	  			out.write(((EditText)findViewById(R.id.editText2)).getText().toString());
	  			out.newLine();
	  			out.write(((EditText)findViewById(R.id.editText3)).getText().toString());
	  			out.newLine();
	  			out.write(((EditText)findViewById(R.id.editText4)).getText().toString());
	  			out.newLine();
	  			out.write(((Spinner)findViewById(R.id.spinner1)).getSelectedItem().toString());
	  			out.newLine();
	  			out.write(((Spinner)findViewById(R.id.spinner2)).getSelectedItem().toString());
	  			out.newLine();
	  			out.close();
	  			fw.flush();
	  			fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
      	  File image = new File(Environment.getExternalStorageDirectory()+"/wpfap/daten_"+name+".jpg");
			Uri uriSavedImage = Uri.fromFile(image);
			ImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
			OutputStream imageFileOS;
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			pictureTaken.compress(Bitmap.CompressFormat.JPEG, 100, stream);
			byte[] byteArray = stream.toByteArray();
			try {
			imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
			imageFileOS.write(byteArray);
			imageFileOS.flush();
			imageFileOS.close();
			} 
			catch (FileNotFoundException e) {
			e.printStackTrace();
			}
			catch (IOException e) {
			e.printStackTrace();
			} 
        	  
          }
        });
    }
              
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (resultCode == Activity.RESULT_OK && requestCode == 0) {
            pictureTaken = (Bitmap) data.getExtras().get("data");
            ImageView im = (ImageView) findViewById(R.id.imageView1);
            im.setImageBitmap(pictureTaken);
    }
}
}

